import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;

public class IdGenerator {
    private final long epoch = 1278920795000L; // 自定义纪元时间，比如系统启动时间
    private long lastTimestamp = 0L;
    private long sequence = -1;

    public String generateId() {
        long currentTimeMillis = System.currentTimeMillis();
        long timePart = (currentTimeMillis - epoch) % 100000000L; // 取时间戳的某部分

        sequence = sequence+1L & 4095L; // 序列号部分，确保在64位内循环
        // 拼接ID
        long idx = RandomUtil.randomInt(1,4);//券类型
//        System.out.println(currentTimeMillis - epoch);
//        System.out.println(timePart);
        long combined = timePart << 12 | sequence;

        lastTimestamp = currentTimeMillis;

        return ""+(combined);
    }

    // 假设这是系统启动时设置的纪元时间
    public static void main(String[] args) {
        IdGenerator generator = new IdGenerator();

        for (int i = 0; i <8192 ; i++) {
            String id = generator.generateId();
            System.out.println("Generated ID: " + id);
        }
        System.out.println(IdUtil.getSnowflake(1, 1).nextId());;
    }
}